package com.ciwong.epaper.modules.scan.util;

import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.epaper.modules.scan.bean.PaperCodeInfo;
import com.ciwong.epaper.modules.scan.dao.ScanDao;

import android.text.Editable;
import android.text.TextWatcher;

/**
 * Created by res-zwyan on 2015/10/22.
 */
public class TextPaperEditChangedListener implements TextWatcher
{
    private String mTestPaperName;

    private ActionResultListener mIResult;

    public final int MAX_CODE_LENGTH = 9;

    public void setActionResultListener(ActionResultListener iResult)
    {
        this.mIResult = iResult;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after)
    {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        int size = s.length();
        if (size == MAX_CODE_LENGTH)
        {
            int codeId = 0;
            try
            {
                codeId = Integer.parseInt(s.toString());
                ScanDao.getInstance().paperCodeByInfo(codeId,
                        new BaseCallBack()
                        {
                            @Override
                            public void failed(int errorCode, Object data)
                            {
                                if (mIResult != null)
                                {
                                    mIResult.onAction(ActionCode.RESULT_ERROR,
                                            String.valueOf(data));
                                }
                            }

                            @Override
                            public void failed(Object data)
                            {
                                if (mIResult != null)
                                {
                                    mIResult.onAction(ActionCode.RESULT_ERROR,
                                            String.valueOf(data));
                                }
                            }

                            @Override
                            public void success(Object data)
                            {
                                PaperCodeInfo paperCodeInfo = (PaperCodeInfo) data;
                                if (mIResult != null)
                                {
                                    mIResult.onAction(
                                            ActionCode.RESULT_SUCCESS, String
                                                    .valueOf(paperCodeInfo
                                                            .getResourceName()));
                                }
                            }
                        });

            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
                if (mIResult != null)
                {
                    mIResult.onAction(ActionCode.RESULT_ERROR, e.getMessage());
                }
            }
        }
        else
        {
            if (mIResult != null)
            {
                mIResult.onAction(ActionCode.MODIFYING, "");
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s)
    {

    }

    public static class ActionCode
    {
        /**
         * 正在网络请求
         */
        public static int READY_REQUEST = 1;

        /**
         * 操作成功
         */
        public static int RESULT_SUCCESS = 2;

        /**
         * 操作失败
         */
        public static int RESULT_ERROR = 3;

        /**
         * 正在修改
         */
        public static int MODIFYING = 4;
    }

    public interface ActionResultListener
    {

        void onAction(int actionCode, String info);
    }
}
